package com.ssyt.blog.core.sys.dao;

import java.util.List;

import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.ssyt.blog.core.common.BaseDao;
import com.ssyt.blog.core.common.PageModel;
import com.ssyt.blog.core.sys.entity.Article;

@Repository
public class ArticleDao extends BaseDao<Article, Integer> {
	public PageModel<Object[]> list(int pageNo) {

		return pageMultiPropertyByHql(
				pageNo,
				5,
				"SELECT a.id,a.title,a.content,a.createTime,a.readTimes,u.name AS userName,u.photo AS photo,u.id AS userId,c.name AS categoryName FROM Article a,User u,Category c WHERE a.userId=u.id AND a.categoryId=c.id order by a.createTime desc");
	}

	public Article getArticle(int id) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT new Article(");
		sb.append("a.id, a.title, a.content, a.createTime, a.readTimes, ");
		sb.append("u.name, u.photo, u.id, ");
		sb.append("c.name, ");
		sb.append("b.name, b.description, b.bgImage) ");
		sb.append("FROM Article a,User u,Blog b,Category c ");
		sb.append("WHERE a.userId=u.id AND b.userId=u.id AND c.userId=u.id ");
		sb.append("AND a.id=?");

		return uniqueEntityByHql(sb.toString(), id);
	}

	public void addReadTimes(int id) {
		Article article = get(id);
		int count = article.getReadTimes() + 1;
		System.out.println(count);
		article.setReadTimes(count);
		update(article);
	}

	public List<Article> allRankList() {

		Query<Article> query = getSession().createQuery(
				"FROM Article order by readTimes desc", Article.class);
		// 设置最大记录数
		query.setMaxResults(10);
		return query.getResultList();
	}

	public List<Article> authorRankList(int userId) {
		Query<Article> query = getSession().createQuery(
				"FROM Article WHERE userId=? order by readTimes desc",
				Article.class);
		query.setParameter(0, userId);
		query.setMaxResults(10);
		return query.getResultList();
	}

	public PageModel<Article> getArticleList(int userId, int pageNo) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT new Article(");
		sb.append("a.id, a.title, a.content, a.createTime, a.readTimes, ");
		sb.append("a.userId,c.name) ");
		sb.append("FROM Article a,Category c ");
		sb.append("WHERE c.id=a.categoryId ");
		sb.append("AND a.userId=? order by a.createTime desc");
		return pageEntityByHql(pageNo, 5, sb.toString(), userId);
	}

	public List<Object[]> categoryInfo(int userId) {
		List<Object[]> list = listMultiPropertyByHql(
				"SELECT c.id , c.name ,COUNT(a.categoryId) FROM Article a,Category c WHERE a.categoryId=c.id AND a.userId=? GROUP BY a.categoryId",
				userId);
		Long count = uniqueSinglePropertyByHql(Long.class,
				"SELECT COUNT(*) FROM Article WHERE userId=?", userId);
		Object[] countArr = { count };
		list.add(countArr);
		return list;
	}

	public PageModel<Article> getArticleByCategory(int categoryId, int userId,
			int pageNo) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT new Article(");
		sb.append("a.id, a.title, a.content, a.createTime, a.readTimes, ");
		sb.append("a.userId,c.name) ");
		sb.append("FROM Article a,Category c ");
		sb.append("WHERE c.id=a.categoryId ");
		sb.append("AND a.userId=? AND a.categoryId=? ");
		sb.append("order by a.createTime desc");
		return pageEntityByHql(pageNo, 5, sb.toString(), userId, categoryId);
	}

	public void articleInfoDelete(Integer id) {
		Article article = load(id);
		delete(article);
	}

	public Integer addArticle(Article article) {
		return save(article);
	}
}
